﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GIMS.BLL;
using KPIS.GERP.GIMS.IBLL;
using KPIS.GERP.GIMS.MODEL;
using System.Data;
using System.IO;

namespace KPIS.GERP.GIMS.APP.APP_UI.Add_Form
{
    public partial class ICM_Purchase_Order_Installment : System.Web.UI.Page
    {
        PurchaseOrderInstallmentItemBLL bllInstallItem = new PurchaseOrderInstallmentItemBLL();
        private bool isItemInEditMode = false;
        double totalAmount = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadGridItem();
                lblInstallNumber.Text = GetInstallSeq().ToString();
            }
        }

        protected int GetBidderSeq()
        {
            int bidder_seq = Convert.ToInt32(Request.QueryString["bidder_seq"]);
            return bidder_seq;
        }

        protected int GetInstallSeq()
        {
            int install_seq = Convert.ToInt32(Request.QueryString["install_seq"]);
            return install_seq;
        }

        protected void LoadGridItem()
        {
            IList<PurchaseOrderInstallmentItemInfo> infoList = new List<PurchaseOrderInstallmentItemInfo>();

            //if (Session["InstallmentItem_" + GetBidderSeq().ToString() + "_" + GetInstallSeq().ToString()] == "" || Session["InstallmentItem_" + GetBidderSeq().ToString() + "_" + GetInstallSeq().ToString()] == null)
            //{
            //    infoList = bllInstallItem.GetBidderQuotationFromBidderForInstallment(GetBidderSeq());
            //}
            //else
            //{
            infoList = (List<PurchaseOrderInstallmentItemInfo>)Session["InstallmentItem_" + GetBidderSeq().ToString() + "_" + GetInstallSeq().ToString()];
            //}

            Grid_Item.DataSource = infoList;
            Grid_Item.DataBind();
            Edit_Item.Visible = true;
            EditSave_Item.Visible = false;
            Cancel_Item.Visible = false;
        }

        protected void Grid_Item_ItemCommand(object source, GridCommandEventArgs e)
        {
            LoadGridItem();
        }

        protected void Grid_Item_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            {
                double price = 0;
                GridDataItem dataBoundItem = e.Item as GridDataItem;

                double pricePerQty = Convert.ToDouble(dataBoundItem["PriceInVat"].Text);
                RadNumericTextBox txtQty = ((RadNumericTextBox)(dataBoundItem["Qty"].FindControl("txtQty")));
                RadNumericTextBox txtNetAmount = ((RadNumericTextBox)(dataBoundItem["NetAmount"].FindControl("txtNetAmount")));

                txtQty.Attributes.Add("onFocus", "return FocusQty('" + txtQty.ClientID + "','" + txtNetAmount.ClientID + "','" + pricePerQty + "')");
                txtQty.Attributes.Add("onBlur", "return BlurQty('" + txtQty.ClientID + "','" + txtNetAmount.ClientID + "','" + pricePerQty + "')");
                txtNetAmount.Attributes.Add("onFocus", "return FocusPrice('" + txtNetAmount.ClientID + "')");
                txtNetAmount.Attributes.Add("onBlur", "return BlurPrice('" + txtNetAmount.ClientID + "')");

                if (((RadNumericTextBox)(dataBoundItem["NetAmount"].FindControl("txtNetAmount"))).Value.ToString().Length > 0)
                {
                    price = double.Parse(((RadNumericTextBox)(dataBoundItem["NetAmount"].FindControl("txtNetAmount"))).Value.ToString());
                }

                totalAmount += price;
            }
            else if (e.Item is GridFooterItem)
            {
                GridFooterItem footer = (GridFooterItem)e.Item;
                ((Label)footer["NetAmount"].FindControl("lblTotalNetAmount")).Text = String.Format("{0:N3}", totalAmount);
                ((RadNumericTextBox)footer["NetAmount"].FindControl("txtTotalNetAmount")).Value = Double.Parse(totalAmount.ToString());
            }
        }

        protected void Edit_Item_Click(object sender, EventArgs e)
        {
            this.IsItemInEditMode = true;
            LoadGridItem();
            Edit_Item.Visible = false;
            EditSave_Item.Visible = true;
            Cancel_Item.Visible = true;
        }

        protected void EditSave_Item_Click(object sender, EventArgs e)
        {
            IList<PurchaseOrderInstallmentItemInfo> infoList = new List<PurchaseOrderInstallmentItemInfo>();
            foreach (GridDataItem data in Grid_Item.MasterTableView.Items)
            {
                PurchaseOrderInstallmentItemInfo info = new PurchaseOrderInstallmentItemInfo();
                PurchaseOrderInstallmentInfo infoInstallment = new PurchaseOrderInstallmentInfo();
                BidderQuotationInfo infoBidderQuotation = new BidderQuotationInfo();
                SysVatInfo infoVat = new SysVatInfo();
                ResourceMasterItemInfo infoItem = new ResourceMasterItemInfo();
                PurchaseRequestItemInfo infoPrItem = new PurchaseRequestItemInfo();
                BidderInfo infoBidder = new BidderInfo();

                string vat_percent = data["InfoBidderQuotation.InfoBidder.VatPercent"].Text;

                infoVat.Seq = Convert.ToInt32(data["InfoBidderQuotation.InfoBidder.InfoVat.Seq"].Text);
                infoVat.Name = data["InfoBidderQuotation.InfoBidder.InfoVat.Name"].Text;
                infoItem.Name = ((Label)data["InfoBidderQuotation.InfoPrItem.InfoItem.Name"].FindControl("lblItem")).Text;
                infoInstallment.Seq = GetInstallSeq();
                infoInstallment.InstallNumber = GetInstallSeq();
                infoBidderQuotation.Seq = Convert.ToInt32(data["InfoBidderQuotation.Seq"].Text);
                info.Descr = ((TextBox)data["Descr"].FindControl("txtDescr")).Text;
                info.Qty = (((RadNumericTextBox)data["Qty"].FindControl("txtQty")).Text.Length > 0
                    ? Convert.ToInt32(((RadNumericTextBox)data["Qty"].FindControl("txtQty")).Text)
                    : 0);
                info.PriceExVat = Convert.ToDouble(data["PriceExVat"].Text);
                info.PriceInVat = Convert.ToDouble(data["PriceInVat"].Text);
                info.NetAmount = (((RadNumericTextBox)data["NetAmount"].FindControl("txtNetAmount")).Text.Length > 0
                    ? Convert.ToInt32(((RadNumericTextBox)data["NetAmount"].FindControl("txtNetAmount")).Text)
                    : 0);
                info.VatAmount = Math.Round(Convert.ToDouble(info.NetAmount * (Convert.ToDouble(vat_percent) / (Convert.ToDouble(vat_percent) + 100))), 3);
                info.ExVatAmount = (info.NetAmount - info.VatAmount);
                infoBidder.VatPercent = Convert.ToDouble(vat_percent);
                infoPrItem.Seq = Convert.ToInt32(data["InfoBidderQuotation.InfoPrItem.Seq"].Text);

                infoPrItem.InfoItem = infoItem;
                infoBidder.InfoVat = infoVat;
                infoBidderQuotation.InfoPrItem = infoPrItem;
                infoBidderQuotation.InfoBidder = infoBidder;
                info.InfoBidderQuotation = infoBidderQuotation;
                info.InfoPoInstallment = infoInstallment;
                infoList.Add(info);
            }
            Session["InstallmentItem_" + GetBidderSeq().ToString() + "_" + GetInstallSeq().ToString()] = infoList;
            LoadGridItem();
        }

        protected void Cancel_Item_Click(object sender, EventArgs e)
        {
            this.IsItemInEditMode = false;
            LoadGridItem();
            Edit_Item.Visible = true;
            EditSave_Item.Visible = false;
            Cancel_Item.Visible = false;
        }

        protected bool IsItemInEditMode
        {
            get { return this.isItemInEditMode; }
            set { this.isItemInEditMode = value; }
        }
    }
}
